import 'dart:ui';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SysInfo {
  static const String SPKeyPrefix = "SysInfo";

  static const String SP_SHOW_LEFT_BAR = "$SPKeyPrefix:showLeftBar";
  static const String SP_APP_NAME = "$SPKeyPrefix:appName";
  static const String SP_PACKAGE_NAME = "$SPKeyPrefix:packageName";
  static const String SP_VERSION = "$SPKeyPrefix:version";
  static const String SP_BUILD_NUMBER = "$SPKeyPrefix:buildNumber";
  static const String SP_DEVICE_SN = "$SPKeyPrefix:deviceSN";
  static const String SP_PASSWORD = "$SPKeyPrefix:password";
  static const String SP_TOKEN_TYPE = "$SPKeyPrefix:tokenType";
  static const String SP_ACCESS_TOKEN = "$SPKeyPrefix:accessToken";
  static const String SP_REFRESH_TOKEN = "$SPKeyPrefix:refreshToken";
  static const String SP_USER_NAME = "$SPKeyPrefix:name";
  static const String SP_USER_ID = "$SPKeyPrefix:userId";
  static const String SP_USER_GENDER = "$SPKeyPrefix:gender";
  static const String SP_USER_AVATAR = "$SPKeyPrefix:avatar";
  static const String SP_FIRST_RUN = "$SPKeyPrefix:firstRun";
  static const String SP_IS_REMEMBER = "$SPKeyPrefix:isRemember";
  static const String SP_NEED_PHOTO = "$SPKeyPrefix:needPhoto";
  static const String SP_AUTO_CLOCK = "$SPKeyPrefix:autoClock";
  static const String SP_CLOCKUSERINFOJSON = "$SPKeyPrefix:clockUserInfoJson";

  SysInfo._privateConstructor();
  static final SysInfo _instance = SysInfo._privateConstructor();
  static SysInfo get instance {
    return _instance;
  }

  bool? autoClock;
  bool? needPhoto;
  bool? isRemember;
  String? userName;
  String? password;
  bool? firstRun;
  String? tokenType;
  String? accessToken;
  String? refreshToken;
  int? expiresIn;
  late Size deviceSize;
  String? clockUserInfoJson;

  String getToken() {
    return tokenType == null || accessToken == null ? "" : tokenType! + accessToken!;
  }

  void upFirst(bool value) {
    firstRun = value;
  }

  bool isLogin() {
    print("用户是否登录******************************: $accessToken");
    return !(accessToken == null || accessToken == "" || accessToken!.length < 1);
//    return false;
  }

  Future clearLogin() async {
    tokenType = null;
    accessToken = null;
    refreshToken = null;
//    await clear();
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove(SP_TOKEN_TYPE);
    await prefs.remove(SP_ACCESS_TOKEN);
    await prefs.remove(SP_REFRESH_TOKEN);
    upFirst(false);
    await save();
    return Future.value();
  }

  void upTheme() async {}

  Future save() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return this.doSave(prefs);
  }

  Future<SysInfo> load() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    this.doLoad(prefs);
    return SysInfo.instance;
  }

  void doLoad(SharedPreferences sp) {
    autoClock = sp.get(SP_AUTO_CLOCK) != null ? sp.getBool(SP_AUTO_CLOCK) : false;
    needPhoto = sp.get(SP_NEED_PHOTO) != null ? sp.getBool(SP_NEED_PHOTO) : false;
    isRemember = sp.get(SP_IS_REMEMBER) != null ? sp.getBool(SP_IS_REMEMBER) : true;
    userName = sp.get(SP_USER_NAME) != null ? sp.getString(SP_USER_NAME) : "";
    password = sp.get(SP_PASSWORD) != null ? sp.getString(SP_PASSWORD) : "";
    tokenType = sp.get(SP_TOKEN_TYPE) != null ? sp.getString(SP_TOKEN_TYPE) : "";
    accessToken = sp.get(SP_ACCESS_TOKEN) != null ? sp.getString(SP_ACCESS_TOKEN) : "";
    refreshToken = sp.get(SP_REFRESH_TOKEN) != null ? sp.getString(SP_REFRESH_TOKEN) : "";
    firstRun = sp.get(SP_FIRST_RUN) != null ? sp.getBool(SP_FIRST_RUN) : true;
    clockUserInfoJson = sp.get(SP_CLOCKUSERINFOJSON) != null ? sp.getString(SP_CLOCKUSERINFOJSON) : "";
  }

  Future doSave(SharedPreferences sp) async {
    await sp.setBool(SP_AUTO_CLOCK, autoClock??false);
    await sp.setBool(SP_NEED_PHOTO, needPhoto??true);
    await sp.setBool(SP_IS_REMEMBER, isRemember??false);
    await sp.setString(SP_USER_NAME, userName??'');
    await sp.setString(SP_PASSWORD, password??'');
    await sp.setString(SP_TOKEN_TYPE, tokenType??'');
    await sp.setString(SP_ACCESS_TOKEN, accessToken??'');
    await sp.setString(SP_REFRESH_TOKEN, refreshToken??'');
    await sp.setBool(SP_FIRST_RUN, firstRun??false);
    await sp.setString(SP_CLOCKUSERINFOJSON, clockUserInfoJson??'');
  }

  Future clear() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.clear();
  }
}
